package com.example.android.ui.me.user.carinfo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.android.R;
import com.example.android.adapter.CarAdapter;
import com.example.android.bean.AddCar;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.BaseModel;
import com.example.android.utils.NetUtil;
import com.google.android.material.snackbar.Snackbar;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class CarFragment extends Fragment {
    private CarAdapter pythonAdapter;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        //获得fragment
        View root = inflater.inflate(R.layout.fragment_car, container, false);
        RecyclerView recyclerView = root.findViewById(R.id.recyclerView);//获得组件
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(root.getContext(), DividerItemDecoration.VERTICAL));
        pythonAdapter = new CarAdapter(null);
        recyclerView.setAdapter(pythonAdapter);
        getlist();
        //设置车辆信息的长按操作事件
        pythonAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            AddCar addCar = pythonAdapter.getData().get(position);
            final String[] items = {"详情", "删除"};
            new AlertDialog.Builder(requireContext())
//                    .setTitle("请选择")
                    .setItems(items, (dialog, which) -> {
                        switch (which) {
                            case 0:
                                //进入车辆详情页面
                                Bundle bundle = new Bundle();
                                bundle.putString("id", String.valueOf(addCar.getId()));
                                Navigation.findNavController(view).navigate(R.id.carinfoFragment, bundle);
                                break;
                            case 1:
                                //删除车辆信息
                                NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class).deleteCar(addCar.getId()).enqueue(new Callback<BaseModel<Object>>() {
                                    @Override
                                    public void onResponse(Call<BaseModel<Object>> call, Response<BaseModel<Object>> response) {
                                        if (response.body().getCode() == 200) {
                                            Snackbar.make(view, "删除成功：", Snackbar.LENGTH_LONG).show();
                                            getlist();
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<BaseModel<Object>> call, Throwable t) {
                                        Snackbar.make(view, "删除失败：" + t.getMessage(), Snackbar.LENGTH_LONG).show();
                                    }
                                });
                                break;
                            default:
                        }
                    }).show();
            return true;
        });
        //开启按钮
        setHasOptionsMenu(true);
        return root;
    }

    private void getlist() {
        CarViewModel pythonViewModel = new ViewModelProvider(this).get(CarViewModel.class);
        pythonViewModel.getCarList().observe(getViewLifecycleOwner(), addCarBaseModelTable -> {
            if (addCarBaseModelTable != null) {
                pythonAdapter.setList(addCarBaseModelTable.getRows());
            }
        });
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        //添加菜单栏的按钮
        inflater.inflate(R.menu.add, menu);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //对菜单栏点击事件重写
        switch (item.getItemId()) {
            case R.id.action_name://如果点击了添加，就进入添加车辆页面，否则返回上一层
                Navigation.findNavController(requireView()).navigate(R.id.action_frahmentCar_to_addFragment);
                break;
            default:
                Navigation.findNavController(requireView()).navigateUp();
        }
        return true;
    }

}